package cn.wolfcode.web.controller;

import cn.wolfcode.domain.File;
import cn.wolfcode.domain.User;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.service.IFileService;
import cn.wolfcode.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class dengluController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/denglu")
    @ResponseBody
    public JSONResult denglu(String username, String password)throws Exception{
        JSONResult jsonResult = new JSONResult();
        System.out.println(username);
        System.out.println(password);
        User selectup = userService.selectup(username, password);
        System.out.println(selectup);
        if(selectup!=null){
            jsonResult.setSuccess(true);
            jsonResult.setMsg("登陆成功");
        }else {
            jsonResult.setSuccess(false);
            jsonResult.setMsg("登陆失败");
        }
        return jsonResult;
    }
    @RequestMapping("/chaxun")
    @ResponseBody
    public List<User> chaxun()throws Exception{
        List<User> sel = userService.sel();
        return sel;
    }
    @RequestMapping("/shachu")
    @ResponseBody
    public void shachu(Long id)throws Exception{
        System.out.println(id);
        userService.dalete(id);
    }
    @RequestMapping("/xiugai")
    @ResponseBody
    public void xiugai(Long id, String username,String password,String age)throws Exception{
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        if(age !=null){
            user.setAge(Integer.parseInt(age));
        }else {
            user.setAge(0);
        }
        System.out.println(user);
        userService.update(user);
    }
    @RequestMapping("/tianjia")
    @ResponseBody
    public void tianjia(String username,String password,String age)throws Exception{
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        if(age !=null){
            user.setAge(Integer.parseInt(age));
        }else {
            user.setAge(0);
        }
        System.out.println(user);
        userService.insert(user);
    }
    @RequestMapping("/huixian")
    @ResponseBody
    public User huixian(Long id)throws Exception{
        return userService.selectid(id);
    }
}
